package com.shengdou.as.slopsapptest.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.shengdou.as.slopsapptest.utils.FileUtils;
import com.shengdou.as.slopsapptest.utils.NetConstant;

import java.io.File;

public class PhoneReceiver extends BroadcastReceiver {

    private static final String TAG = "PhoneReceiver";

    public PhoneReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action == null) {
            return;
        }
        if (action.equals(NetConstant.VERSION)) {
            try {
                if (BuildConfigs.DEBUG) {
                    Log.e(TAG, "开始安装");
                }
                File file = FileUtils.getDownAppFile(context);
                Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent2.setDataAndType(
                        Uri.fromFile(file),
                        "application/vnd.android.package-archive");
                context.startActivity(intent2);
            } catch (Exception e) {
                if (BuildConfigs.DEBUG) {
                    Log.e(TAG, "安装出错");
                }
                e.printStackTrace();
            }
        }
    }
}
